package cn.com.yixing.loginregistration.config;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;


import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages ={"cn.com.yixing.jianlianfriendscircle.secound.mapper"},sqlSessionFactoryRef = "salverSqlSessionFactory")
public class testDataSourceConfig {

    //springboot多数据源配置
    @Bean(name = "salver")
    @ConfigurationProperties(prefix = "spring.datasource.salver")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }
    //事务

    @Bean(name="salverTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("salver")DataSource dataSource){
        return  new DataSourceTransactionManager(dataSource);
    }
    //配置

    @Bean(name = "salverSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("salver")DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean=new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("cn.com.yixing.jianlianfriendscircle.secound.model");
        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            factoryBean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*.xml"));
            return factoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
//        return factoryBean.getObject();
    }
}
